package com.fingard.dsp.bank.directbank.wzcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 温州银行余额查询
 */
public class WZCB01Balance extends WZCB01Base implements IBalance {

    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //拼接请求报文体
                WZCB01Header header = new WZCB01Header();
                header.txCode="E102";//SQEPay.102.01.01
                header.orderNo = getQryTransID();
                XmlTextWriter xmlReq = new XmlTextWriter();
                writeReqHeader(xmlReq,header);
                XElement body =new XElement("Body","");
                body.addChild(new XElement("BusCode","1"));//保留域填 ‘1’
                body.addChild(new XElement("PayAcctNo",p_req7506.readedDetail.actNum));//查询账号
                body.addChild(new XElement("PayAcctSeqno","0"));//必填 0 （保留为接口扩展用）
                xmlReq.writeXElement(body);
                xmlReq.writeEndDocument();
                //组装head
                String head=getHead("E102","SQEPay.102.01.01");
                //发送请求报文
                String[] bankResult = sendToBank(xmlReq.toXmlString(),head);
                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String respCode = tmpBkReader.getFirstText("/Response/Head/RefuseCode");
                        String respInfo = tmpBkReader.getFirstText("/Response/Head/RefuseReason");
                        if ("0000".equals(respCode)) {//交易成功
                            if (tmpBkReader.read("/Response/Body")) {
                                detail.respCode = "0";
                                detail.respInfo = respInfo;
                                detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("CurCode"));
                                detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("PayAcctNo");
                                detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("PayAcctBal");
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = respInfo;
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        }catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
